package com.java110.api.listener.electric;

import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.intf.meter.IMeterInnerServiceSMO;
import com.java110.utils.constant.lm.service.code.WeEnum;
import com.java110.vo.ResultVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Java110Listener("weMeterDownListener")
public class WeMeterDownListener extends AbstractServiceApiListener {

    @Autowired
    private IMeterInnerServiceSMO meterInnerServiceSMO;

    private static Logger logger = LoggerFactory.getLogger(WeMeterDownListener.class);
    @Override
    public String getServiceCode() {
        return WeEnum.we_meter_down.getCode();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        logger.debug("WeMeterDownListener : {}", event);
        String code = reqJson.getString("code");
        ResultVo vo = new ResultVo();
        if(StringUtils.isNotBlank(code)){
            String json = meterInnerServiceSMO.down(code);
            if(StringUtils.isNotBlank(json)){
                JSONObject object = JSONObject.parseObject(json);
                Integer code1 = object.getInteger("code");
                if(code1 == 0){
                    vo.setMsg("ok");
                    vo.setCode(0);
                    ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
                    context.setResponseEntity(responseEntity);
                } else {
                    vo.setCode(-1);
                    vo.setMsg(object.getString("msg"));
                    ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
                    context.setResponseEntity(responseEntity);
                }
            } else {
                vo.setCode(-1);
                vo.setMsg("上游系统错误");
                ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
                context.setResponseEntity(responseEntity);
            }
        } else {
            vo.setCode(-1);
            vo.setMsg("设备码错误");
            ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
            context.setResponseEntity(responseEntity);
        }
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
